<?php

class Pagination {   

	function __construct(&$locator) {
		$this->locator =& $locator;
  	}

	function setPage($prefix) {
		$request	=& $this->locator->get('request');
		$session	=& $this->locator->get('session');

        $session->set($prefix.'.page', $request->has('page')?$request->get('page'):1);
		return $session->get($prefix.'.page');
	}

	function getPage($prefix) {
		$session	=& $this->locator->get('session');

		return $session->get($prefix.'.page');
	}

	function getPageData($controller,$query=array()) {
		$request	=& $this->locator->get('request');
		$language	=& $this->locator->get('language');
		$url		=& $this->locator->get('url');
		$database	=& $this->locator->get('database');

		$page_data = array();

		for ($i = 1; $i <= $database->pages; $i++) {
			if ($i > 1) { $query['page'] = $i; }
			$page_data[] = array(
				'text'  => $language->get('text_pages', $i, $database->getPages()),
				'href'	=> $url->href($controller, FALSE, $query),
				'value' => $i
			);
		}
		return $page_data;
	}

	function render($data=false) {
		$template	=& $this->locator->get('template');

		$view = $this->locator->create('template');

		if ($data) { $view->set($data); }

		return $view->fetch('module/pagination.tpl');
	}
}

?>